import { getObjType, rgbTohex } from '../utils/util';
import { getSheetIndex } from '../methods/get';
import server from '../controllers/server';
import formula from './formula';
import editor from './editor';
import { dynamicArrayCompute } from './dynamicArray';
import sheetmanage from '../controllers/sheetmanage';
import { isInlineStringCT, isInlineStringCell, convertCssToStyleList } from '../controllers/inlineString';
import locale from '../locale/locale';
import Store from '../store';

//Get selection range value
export function getdatabyselection_old(range, sheetIndex) {
    if (range == null) {
        range = Store.luckysheet_select_save[0];
    }

    if (range["row"] == null || range["row"].length == 0) {
        return [];
    }

    //取数据
    let d, cfg;
    if (sheetIndex != null && sheetIndex != Store.currentSheetIndex) {
        d = Store.luckysheetfile[getSheetIndex(sheetIndex)]["data"];
        cfg = Store.luckysheetfile[getSheetIndex(sheetIndex)]["config"];
    }
    else {
        d = editor.deepCopyFlowData(Store.flowdata);
        if(d.length==0){
            d = Store.luckysheetfile[getSheetIndex(sheetIndex)]["data"];
        }
        cfg = Store.config;
    }

    let data = [];

    for (let r = range["row"][0]; r <= range["row"][1]; r++) {
        if (d[r] == null) {
            continue;
        }

        if (cfg["rowhidden"] != null && cfg["rowhidden"][r] != null) {
            continue;
        }

        let row = [];

        for (let c = range["column"][0]; c <= range["column"][1]; c++) {
            row.push(d[r][c]);
        }

        data.push(row);
    }

    // nby 只有s字段的情况添加v和m
    data = data.map(arr => arr.map(cell => {
        let s = cell?.ct?.s
        if (s && cell.v == undefined) {
            s = s.map(item => item?.v).join('')
            cell.v = s
            cell.m = s
        }
        return cell
    }))
    // nby 只有s字段的情况添加v和m END
    return data;
}
// nby 需要允许图表跨行选取数据  上面是源码
export function getdatabyselection(range, sheetIndex, isArray) {
    if(isArray){
        let arr = []
       arr = range.map(range=>{
              //取数据
            let d, cfg;
            if (sheetIndex != null && sheetIndex != Store.currentSheetIndex) {
                d = Store.luckysheetfile[getSheetIndex(sheetIndex)]["data"];
                cfg = Store.luckysheetfile[getSheetIndex(sheetIndex)]["config"];
            }
            else {
                d = editor.deepCopyFlowData(Store.flowdata);
                cfg = Store.config;
            }
        
            let data = [];
        
            for (let r = range["row"][0]; r <= range["row"][1]; r++) {
                if (d[r] == null) {
                    continue;
                }
        
                if (cfg["rowhidden"] != null && cfg["rowhidden"][r] != null) {
                    continue;
                }
        
                let row = [];
        
                for (let c = range["column"][0]; c <= range["column"][1]; c++) {
                    row.push(d[r][c]);
                }
        
                data.push(row);
            }
        
            // nby 只有s字段的情况添加v和m
            data = data.map(arr => arr.map(cell => {
                let s = cell?.ct?.s
                if (s && cell.v == undefined) {
                    s = s.map(item => item?.v).join('')
                    cell.v = s
                    cell.m = s
                }
                return cell
            }))
            // nby 只有s字段的情况添加v和m END
            return data;
        })
        arr = arr.flat()
        return arr
    }else{
        if (range == null) {
            range = Store.luckysheet_select_save[0];
        }
    
        if (range["row"] == null || range["row"].length == 0) {
            return [];
        }
    
        //取数据
        let d, cfg;
        if (sheetIndex != null && sheetIndex != Store.currentSheetIndex) {
            d = Store.luckysheetfile[getSheetIndex(sheetIndex)]["data"];
            cfg = Store.luckysheetfile[getSheetIndex(sheetIndex)]["config"];
        }
        else {
            d = editor.deepCopyFlowData(Store.flowdata);
            cfg = Store.config;
        }
    
        let data = [];
    
        for (let r = range["row"][0]; r <= range["row"][1]; r++) {
            if (d[r] == null) {
                continue;
            }
    
            if (cfg["rowhidden"] != null && cfg["rowhidden"][r] != null) {
                continue;
            }
    
            let row = [];
    
            for (let c = range["column"][0]; c <= range["column"][1]; c++) {
                row.push(d[r][c]);
            }
    
            data.push(row);
        }
    
        // nby 只有s字段的情况添加v和m
        data = data.map(arr => arr.map(cell => {
            let s = cell?.ct?.s
            if (s && cell.v == undefined) {
                s = s.map(item => item?.v).join('')
                cell.v = s
                cell.m = s
            }
            return cell
        }))
        // nby 只有s字段的情况添加v和m END
        return data;
    }
  
}

export function getdatabyselectionD(d, range) {
    if (range == null || range["row"] == null || range["row"].length == 0) {
        return [];
    }

    let dynamicArray_compute = dynamicArrayCompute(Store.luckysheetfile[getSheetIndex(Store.currentSheetIndex)]["dynamicArray"]);
    let data = [];

    if (d == null) {
        return data;
    }

    for (let r = range["row"][0]; r <= range["row"][1]; r++) {
        if (d[r] == null) {
            continue;
        }

        // if (Store.config["rowhidden"] != null && Store.config["rowhidden"][r] != null) {
        //     continue;
        // }

        let row = [];

        for (let c = range["column"][0]; c <= range["column"][1]; c++) {
            let value;

            if ((r + "_" + c) in dynamicArray_compute) {
                value = dynamicArray_compute[r + "_" + c];
            }
            else {
                value = d[r][c];
            }

            row.push(value);
        }

        data.push(row);
    }

    return data;
}

export function getdatabyselectionNoCopy(range) {
    if (range == null || range["row"] == null || range["row"].length == 0) {
        return [];
    }

    let data = [];

    for (let r = range["row"][0]; r <= range["row"][1]; r++) {
        let row = [];

        if (Store.config["rowhidden"] != null && Store.config["rowhidden"][r] != null) {
            continue;
        }

        for (let c = range["column"][0]; c <= range["column"][1]; c++) {
            let value = "";

            if (Store.flowdata[r] != null && Store.flowdata[r][c] != null) {
                value = Store.flowdata[r][c];
            }

            // nby 只要值是空就不计数，即使单元格有格式，（这样就与excel保持一致）
            if (value?.v == null) {
                value = ''
            }
            // nby 只要值是空就不计数，即使单元格有格式，（这样就与excel保持一致） END
            row.push(value);
        }

        data.push(row);
    }

    return data;
}

//Get the value of the cell
export function getcellvalue(r, c, data, type,isD) {
    if (type == null) {
        type = "v";
    }

    if (data == null) {
        data = Store.flowdata;
    }

    let d_value;

    if (r != null && c != null) {
        d_value = data[r][c];
    }
    else if (r != null) {
        d_value = data[r];
    }
    else if (c != null) {
        let newData = data[0].map(function (col, i) {
            return data.map(function (row) {
                return row[i];
            })
        });
        d_value = newData[c];
    }
    else {
        return data;
    }

    let retv = d_value;

    if (getObjType(d_value) == "object") {
        retv = d_value[type];

        if (type == "f" && retv != null) {
            retv = formula.functionHTMLGenerate(retv);
        }
        else if (type == "f") {
            retv = d_value["v"];
        }
        // else if(isD && Store.isCopyTextOfDate){
        //     retv = d_value[type];
        // }
        else if(type === 'v'){
            // nby 2024-04-04  日期格式要v给v
            retv = d_value.v;
        }
        else if (d_value && d_value.ct && d_value.ct.t == 'd' && type !='v') {
            retv = d_value.m;
        }
    }

    if (retv == undefined) {
        retv = null;
    }

    return retv;
}

//Data increase in rows and columns
export function datagridgrowth(data, addr, addc, iscallback) {
    if (addr <= 0 && addc <= 0) {
        return data;
    }

    if (addr <= 0) {
        addr = 0;
    }

    if (addc <= 0) {
        addc = 0;
    }

    let dataClen = 0;
    if (data.length == 0) {
        data = [];
        dataClen = 0;
    }
    else {
        dataClen = data[0].length;
    }

    let coladd = [];//需要额外增加的空列
    for (let c = 0; c < addc; c++) {
        coladd.push(null);
    }

    let rowadd = [];//完整的一个空行
    for (let r = 0; r < dataClen + addc; r++) {
        rowadd.push(null);
    }

    // 2022.11.15 jpsoft 大数据量情况时，以下for循环非常耗时，加上if判断后，1万行10列数据的渲染时间从15秒下降到几百毫秒
    //  if (coladd && coladd.length) {
    //     for (let r = 0; r < data.length; r++) {
    //         data[r] = [].concat(data[r].concat(coladd));
    //     }
    // }
    //上面的源码：
    for (let r = 0; r < data.length; r++) {
        data[r] = [].concat(data[r].concat(coladd));
    }

    for (let r = 0; r < addr; r++) {
        data.push([].concat(rowadd));
    }

    if (!!iscallback) {
        server.saveParam("all", Store.currentSheetIndex, data.length, { "k": "row" });
        server.saveParam("all", Store.currentSheetIndex, data[0].length, { "k": "column" });
    }

    return data;
}


//Get the formula of the cell
export function getcellFormula(r, c, i, data) {
    let cell;
    if (data != null) {
        cell = data[r][c];
    }
    else {
        cell = getOrigincell(r, c, i);
    }


    if (cell == null) {
        return null;
    }

    return cell.f;
}


export function getOrigincell(r, c, i) {
    if (r == null || c == null) {
        return;
    }
    let data;
    if (i == null) {
        data = Store.flowdata;
    }
    else {
        let sheet = sheetmanage.getSheetByIndex(i);
        data = sheet.data;
    }

    if (!data || !data[r] || !data[r][c]) {
        return;
    }

    return data[r][c];


}

export function getRealCellValue(r, c) {
    let value = getcellvalue(r, c, null, "m");
    if (value == null) {
        value = getcellvalue(r, c);
        if (value == null) {
            let ct = getcellvalue(r, c, null, "ct");
            if (isInlineStringCT(ct)) {
                value = ct.s;
            }
        }
    }

    return value;
}

export function getInlineStringNoStyle(r, c) {
    let ct = getcellvalue(r, c, null, "ct");
    if (isInlineStringCT(ct)) {
        let strings = ct.s, value = "";
        for (let i = 0; i < strings.length; i++) {
            let strObj = strings[i];
            if (strObj.v != null) {
                value += strObj.v;
            }
        }
        return value;
    }

    return "";
}

export function getInlineStringStyle(r, c, data) {
    let ct = getcellvalue(r, c, data, "ct");
    if (data == null) {
        data = Store.flowdata;
    }
    let cell = data[r][c];
    if (isInlineStringCT(ct)) {
        let strings = ct.s, value = "";
        for (let i = 0; i < strings.length; i++) {
            let strObj = strings[i];
            if (strObj.v != null) {
                let style = getFontStyleByCell(strObj);
                /*        源码      
                value += "<span index='"+ i +"' style='"+ style +"'>" + strObj.v + "</span>"; */
                /* nby 支持上标 */
                if (strObj.scriptType === 1) {
                    value += "<span class='superScript' index='" + i + "' style='" + style + "'>" + strObj.v + "</span>";
                } else if (strObj.scriptType === 2) {
                    value += "<span class='subScript' index='" + i + "' style='" + style + "'>" + strObj.v + "</span>";
                }
                else {
                    value += "<span index='" + i + "' style='" + style + "'>" + strObj.v + "</span>";
                }
                /* nby 支持上标 END */
            }
        }
        return value;
    }

    return "";
}

export function getFontStyleByCell(cell, checksAF, checksCF, isCheck = true) {
    if (cell == null) {
        return;
    }
    let style = "";
    const _locale = locale();
    const locale_fontarray = _locale.fontarray;
    for (let key in cell) {
        let value = cell[key];
        if (isCheck) {
            value = checkstatusByCell(cell, key);
        }
        if (key == "bl" && value != "0") {
            style += "font-weight: bold;";
        }

        if (key == "it" && value != "0") {
            style += "font-style:italic;";
        }

        if (key == "ff") {
            let f = value;
            if (!isNaN(parseInt(value))) {
                f = locale_fontarray[parseInt(value)];
            }
            else {
                f = value;
            }
            style += "font-family: " + f + ";";
        }

        if (key == "fs" && value != "10") {
            style += "font-size: " + value + "pt;";
        }

        if ((key == "fc" && value != "#000000") || checksAF != null || (checksCF != null && checksCF["textColor"] != null)) {
            if (checksCF != null && checksCF["textColor"] != null) {
                style += "color: " + checksCF["textColor"] + ";";
            }
            else if (checksAF != null) {
                style += "color: " + checksAF[0] + ";";
            }
            else {
                style += "color: " + value + ";";
            }
        }

        if (key == "cl" && value != "0") {
            style += "text-decoration: line-through;";
        }

        if (key == "un" && (value == "1" || value == "3")) {
            let color = cell["_color"];
            if (color == null) {
                color = cell["fc"];
            }
            let fs = cell["_fontSize"];
            if (fs == null) {
                fs = cell["fs"];
            }
            style += "border-bottom: " + Math.floor(fs / 9) + "px solid " + color + ";";
        }

    }
    return style;
}

export function checkstatusByCell(cell, a) {
    let foucsStatus = cell;
    let tf = { "bl": 1, "it": 1, "ff": 1, "cl": 1, "un": 1 };

    if (a in tf || (a == "fs" && isInlineStringCell(cell))) {
        if (foucsStatus == null) {
            foucsStatus = "0";
        }
        else {
            // var  w = window.getSelection(), isInlineEdit=false; 
            // if(w.type!="None"){
            //     var range = w.getRangeAt(0);
            //     let startContainer = range.startContainer;
            //     if (parseInt($("#luckysheet-input-box").css("top")) > 0 && startContainer.parentNode.tagName=="SPAN" && !range.collapsed) {
            //         let span = startContainer.parentNode;
            //         let styleList = convertCssToStyleList(span.style.cssText);
            //         foucsStatus = styleList[a];
            //         isInlineEdit = true;
            //     }
            // }

            // if(!isInlineEdit){       
            //     if(isInlineStringCell(cell)){
            //         foucsStatus = cell.ct.s[0][a];
            //     }
            //     else{
            //         foucsStatus = foucsStatus[a];
            //     }
            // }   

            foucsStatus = foucsStatus[a];

            if (foucsStatus == null) {
                foucsStatus = "0";
            }
        }
    }
    else if (a == "fc") {
        if (foucsStatus == null) {
            foucsStatus = "#000000";
        }
        else {
            foucsStatus = foucsStatus[a];

            if (foucsStatus == null) {
                foucsStatus = "#000000";
            }

            if (foucsStatus.indexOf("rgba") > -1) {
                foucsStatus = rgbTohex(foucsStatus);
            }
        }
    }
    else if (a == "bg") {
        if (foucsStatus == null) {
            foucsStatus = null;
        }
        else {
            foucsStatus = foucsStatus[a];

            if (foucsStatus == null) {
                foucsStatus = null;
            }
            else if (foucsStatus.toString().indexOf("rgba") > -1) {
                foucsStatus = rgbTohex(foucsStatus);
            }
        }
    }
    else if (a.substr(0, 2) == "bs") {
        if (foucsStatus == null) {
            foucsStatus = "none";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "none";
            }
        }
    }
    else if (a.substr(0, 2) == "bc") {
        if (foucsStatus == null) {
            foucsStatus = "#000000";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "#000000";
            }
        }
    }
    else if (a == "ht") {
        if (foucsStatus == null) {
            foucsStatus = "1";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "1";
            }
        }

        if (["0", "1", "2"].indexOf(foucsStatus.toString()) == -1) {
            foucsStatus = "1";
        }
    }
    else if (a == "vt") {//默认垂直居中
        if (foucsStatus == null) {
            foucsStatus = "0";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "0";
            }
        }

        if (["0", "1", "2"].indexOf(foucsStatus.toString()) == -1) {
            foucsStatus = "0";
        }
    }
    else if (a == "ct") {
        if (foucsStatus == null) {
            foucsStatus = null;
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = null;
            }
        }
    }
    else if (a == "fs") {
        if (foucsStatus == null) {
            foucsStatus = "10";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "10";
            }
        }
    }
    else if (a == "tb") {
        if (foucsStatus == null) {
            foucsStatus = "0";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "0";
            }
        }
    }
    else if (a == "tr") {
        if (foucsStatus == null) {
            foucsStatus = "0";
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = "0";
            }
        }
    }
    else if (a == "rt") {
        if (foucsStatus == null) {
            foucsStatus = null;
        }
        else {
            foucsStatus = foucsStatus[a];
            if (foucsStatus == null) {
                foucsStatus = null;
            }
        }
    }

    return foucsStatus;
}

export function textTrim(x) {
    if (x == null || x.length == 0) {
        return x;
    }
    return x.replace(/^\s+|\s+$/gm, '');
}